﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using Ninject;
using Quester.BL.Entities;
using Quester.BL.Repositories.Abstract;

namespace Quester.Web.IC
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
            ? null
            : (IController)ninjectKernel.Get(controllerType);
        } 

        private void AddBindings()
        {
            //Mock implementation of the ITestRepository
            Mock<ITestRepository> mockTests = new Mock<ITestRepository>();
            mockTests.Setup(t => t.Tests).Returns(new List<TestEntity>() 
            {
                new TestEntity{TestID = Guid.NewGuid(), TestName = "Test 1"},
                new TestEntity{TestID = Guid.NewGuid(), TestName = "Test 2"},
                new TestEntity{TestID = Guid.NewGuid(), TestName = "Test 3"},
                new TestEntity{TestID = Guid.NewGuid(), TestName = "Test 4"},
                new TestEntity{TestID = Guid.NewGuid(), TestName = "Test 5"}
            }.AsQueryable());

            ninjectKernel.Bind<ITestRepository>().ToConstant(mockTests.Object);
        }

    }
}